export const prefix="zi-"
export function t(){
	return prefix
}
// import "./IndexedDB"
/**
 * 
 * 1弧度=π/180
 * 1度=180/π
 * 一弧度等于π除180
 * 一度等于180/π
 * @param {弧度} radians
 */
export function radiansToDegrees(radians) {//弧度到度数
    return 180/Math.PI*radians
}
/**
 * 
 * @param {度数} degrees 
 */
export function degreesToRadians(degrees) {//度数到弧度
    return Math.PI/180*degrees
}

//异步深拷贝
export function asyncCloneDeep(obj){
	return new Promise((r)=>{
		try {
			const {port1,port2} = new MessageChannel();
			port1.postMessage(obj)
			port2.onmessage=mes=>{
				r(mes.data)
			}
		} catch (error) {//拷贝失败使用普通方式进行拷贝
			r(cloneDeep(obj))
		}
	})
}
//深拷贝
export function cloneArray(arr) {
	let arr1=arr.map(i=>{
		if(isArray(i)){
			return cloneArray(i)
		}else if(isObject(i)){
			return cloneObject(i)
		}else{
			return i
		}
	})
	return arr1
}
export function cloneObject(obj) {
	let obj2={}
    Object.entries(obj).forEach(i=>{
		const value=i[1],key=i[0]
		if(isArray(value)){
			obj2[key]=cloneArray(value)
		}else if(isObject(value)){
			obj2[key]=cloneObject(value)
		}else{
			obj2[key]=value
		}
    })
	return obj2
}
export function isArray(obj) {
	return Array.isArray(obj)||Object.prototype.toString.call(obj) === '[object Array]'
}
export function isObject(obj){
	return Object.prototype.toString.call(obj) === '[object Object]'
}
export function cloneDeep(obj) {
	let newObj=null
	if(isArray(obj)){
		newObj=cloneArray(obj)
	}else if(isObject(obj)){
		newObj=cloneObject(obj)
	}else{
		newObj=JSON.parse(JSON.stringify(obj))
	}
	return newObj
}

// let obj={a:1,b:[{c:[1]}]}
// let obj2=await asyncCloneDeep(obj)
// obj2.b[0].c[0]=111
// obj.b[0].c[0]
/**
 * 随机id
 * @param {*} separator 分隔符默认"-" 
 */
export function uid(separator="-"){
	if(!crypto&&crypto.randomUUID){
		return crypto.randomUUID();
	}else{
		const str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
		const arr=[]
		for (let index = 0; index < 3; index++) {
			const a=[]
			for (let i = 0; i < 4; i++) {
				a.push(str[Math.floor(Math.random()*52)])
			}
			arr.push(a.join(""))
		}
		const t=(Date.now()+"").slice(-5)+separator+(Math.random()+"").slice(-5)
		return arr.join(separator)+separator+t
	}
}

export class Bus {  
	constructor() {  
	  this.callbacks = {};   // 存放事件的名字  
	}  
	$on(name, fn) {  
	  this.callbacks[name] = this.callbacks[name] || [];  
	  this.callbacks[name].push(fn);  
	}  
	$emit(name, args) {  
	  if (this.callbacks[name]) {  
		this.callbacks[name].forEach((cb) => cb(args));  
	  }  
	}  
} 